﻿/**************************************************************************
创建时间:	2020/6/28
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using ZhCun.DbCore.Cores;
using ZhCun.DbCore.Entitys;
using ZhCun.Utils;
using ZhCun.Utils.Helpers;
using ZhCun.WebUtils.Configs;

namespace ZhCun.WebUtils.DB
{
    public interface ILoginSessionSet
    {
        void SetLoginSession(ILoginSession session);
    }

    public class BLLBase : IApiResultExtend, ILoginSessionSet
    {
        public BLLBase()
        { }

        DBMain _DB;

        public virtual DBMain DB
        {
            get
            {
                if (_DB == null)
                {
                    _DB = new DBMain();
                }
                return _DB;
            }
        }

        public void SetLoginSession(ILoginSession session)
        {
            DB.LoginSession = session;
        }

        /// <summary>
        /// 当前登陆用户Id
        /// </summary>
        public string CurrUserId
        {
            get
            {
                return DB.LoginSession?.CurrUserId;
            }
        }

        /// <summary>
        /// 当前登陆用户Id
        /// </summary>
        public string CurrRoleId
        {
            get
            {
                return DB.LoginSession?.CurrRoleId;
            }
        }
        /// <summary>
        /// 是否管理员
        /// </summary>
        public bool IsManager
        {
            get
            {
                return CommonConfig.THIS.ManageUserId == CurrUserId;
            }
        }

        /// <summary>
        /// 创建一个自定义Guid
        /// </summary>
        public string NewId()
        {
            return GuidHelper.NewId();
        }

        public DateTime GetNowTime()
        {
            return DateTime.Now;
        }

        public virtual void WriteLog(string log)
        {
            //TODO: 写入数据库日志
        }

        public virtual List<TEntity> GetPagerList<TEntity>(int pageNo, int pageSize, out int total, Action<QueryCondition<TEntity>> QueryHandle)
            where TEntity : EntityBase, new()
        {
            var query = DB.CreateQuery<TEntity>();
            query.PageNo = pageNo;
            query.PageSize = pageSize;
            QueryHandle(query);
            var rList = DB.Query<TEntity>(query).ToList(true);
            total = query.Total;
            return rList;
        }

    }
}